# MessagesPlaceholder 把多轮对话变成模板

from langchain.prompts import (
    ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder
)
from langchain_core.messages import AIMessage, HumanMessage
from langchain_deepseek import ChatDeepSeek

humnan_prompt = "Translate your answer to {language}"
human_message_template = HumanMessagePromptTemplate.from_template(humnan_prompt)

chat_prompt = ChatPromptTemplate.from_messages(
    # variable_name 是 message placeholder 在模板中的变量名
    # 用于在赋值时使用
    [
        MessagesPlaceholder(variable_name="history"),
        human_message_template
    ]
)

human_message = HumanMessage(content="Who is Elon Musk?")
ai_message = AIMessage(content="Elon Musk is a business magnate and investor.")

messages = chat_prompt.format_prompt(
    # history 是 message placeholder 在模板中的变量名; 用于在赋值时使用
    history=[human_message, ai_message], language="中文")

print(messages.to_messages())

llm = ChatDeepSeek(model="deepseek-chat")
result = llm.invoke(messages)

print(result.content)